<?php

require_once '../include/common.inc.php';

if (!checkAdmin()) {
    header("LOCATION:index.php");
    exit;
}

$menu_index = 3;
$page_title = 'JANSEN';
$guide_message = 'Info | Distribution';
$out_ary = array();
$alert_message = '';

$key = "Enter Country...";
getMyParam('action');
getMyParam('key');

if ($action == "add" || $action == "edit") {
    getMyParam('did');
    getMyParam('area');
    getMyParam('country');
    getMyParam('type');
	getMyParam('countryzh');
	getMyParam('typezh');
    getMyParam('seq');

    if ($action == "edit" && (!is_numeric($did) || $did <= 0)) {
        $alert_message .= "\n\nFailed.";
    }
    
    if (!preg_match("/^[0-9]{0,4}$/i", $seq)) {
        $alert_message .= "<p>You have specified an invalid Sequence.</p>";
    }
    
    if ($alert_message == "") {

        if ($action == "add") {
			$sql = "INSERT INTO `distribution` (`area`, `country`, `type`,`countryzh`,`typezh`, `seq`) VALUES ('$area', '$country', '$type','$countryzh', '$typezh', '$seq')";
            $db->query($sql);
        }

        if ($action == "edit") {

            $sql = "UPDATE `distribution` SET
                   `country` = '$country',
				   `countryzh` = '$countryzh',  
                   `area` = '$area', 
                   `type` = '$type', 
					`typezh` = '$typezh',                    
                   `seq` = '$seq'
                   WHERE `id` = '$did'";

            $db->query($sql);
            
            goFromPage();
        }

        header("LOCATION:i_distribution.php");
        exit;
    }

}

if ($action == "del") {
    getMyParam('did');
    if (is_numeric($did) && $did > 0) {
        $sql = "DELETE FROM `distribution` WHERE `id` = '" . $did . "'";
        $db->query($sql);
       header("LOCATION:i_distribution.php");
    }
    
    goFromPage();
}

$sql_where = "";

$page_url = 'i_distribution.php?';
if ($action == "search" && trim($key) != "") {
    $sql_where .= " WHERE `country` LIKE '%$key%'";
    $page_url = 'i_distribution.php?action=search&key=' . $key . '&';
}


// *** page code start ***
$sql = "SELECT `id` FROM `distribution` $sql_where";
$query = $db->query($sql);
$sql_all_num = $db->num_rows($query);

$ppp = 20;
getMyParam('page');
$page = empty($page) || !ispage($page) ? 1 : $page;
$start_limit = ($page - 1) * $ppp;
if($start_limit > $sql_all_num) {
    $start_limit = 0;
    $page = 1;
}

$page_show = getPageStr($sql_all_num, $page, $ppp, $page_url);

// *** page code end ***

$sql = "SELECT `id`, `area`, `country`, `type`,`countryzh`,`typezh`, `seq` FROM `distribution` $sql_where ORDER BY seq ASC, id DESC LIMIT $start_limit, $ppp";
$query = $db->query($sql);
while($rs = $db->fetch_array($query)) {
    if ($action == "search" && trim($key) != "") {
        $rs['country'] = str_replace($key, "<font color=red>$key</font>", $rs['country']);
    }
    $out_ary[] = $rs;
}



$debug = 1;
debuginfo();

include_once(JANSEN_ROOT . 'admin/menu_list.php');
require_once PrintEot('a_header');
require_once PrintEot('a_i_distribution');
require_once PrintEot('a_footer');

?>